<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-图书</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/book.css"/>
  </head>
  
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper clear">
     <div class="book-left clear">
         <%@ include file="/base/jsp/book-type-nav.jsp"%>
         <%@ include file="/base/jsp/hot-books.jsp"%>
     </div>
     
     <div class="book-types-content clear">
         <div class="book-types-content-nav clear">
              
         </div>
         <div class="book-types-content-books clear">
         </div>
        <input name="tid" id="tid" type="text" class="hide"/>
        <input name="tcode" id="tcode" type="text" class="hide"/>
        <input name="ccode" id="ccode" type="text" class="hide"/>
         <%@ include file="/base/jsp/page.jsp"%>
     </div>
  </div>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
 <script type="text/javascript">
  var pageIndex = 0;
  var pageSize = 16;
  var pageT = 0;
  var tid = getParameter("tid");
  var tcode = getParameter("tcode");
  if(isDefine(tid) && isDefine(tcode)){
      $("input[name=tid]").val(tid);
      $("input[name=tcode]").val(tcode);
  }else{
      location.href="webindex.do";
  }
  
  function getBooks(code,pageSize,append){
    var query = "typeCode="+$("input[name=tcode]").val()+"&categoryCode="+code+"&pageIndex="+pageIndex+"&pageSize="+pageSize;
    //热门图书
	$(".type-book").remove();
	request("listBook.do",query,function(data){
		var d = data.data;
		for(var i in d){
			var row = $('<div class="type-book"><div class="book-content"><a target="_blank"><img/></a>'
			+' <div class="book-name"><a target="_blank"></a></div><div class="book-price"><span class="now-price"></span>'
			+'<span class="ori-price"></span></div></div></div>');
            var book = d[i];
            row.find(".book-content a").attr("href","bookdetail.do?id="+book.id);
            row.find(".book-content a img").attr("src",getImg(book.img));
            row.find(".book-name a").text(book.name);
            row.find(".book-name a").attr("href","bookdetail.do?id="+book.id);
            row.find(".now-price").text("￥"+book.priceSelling);
            row.find(".ori-price").text("￥"+book.priceOriginal);
            $(".book-types-content-books").append(row);
		}
		var total = data.total;
		//计算总页数
		pageT = Math.ceil(total / pageSize);
		//对分页进行处理
		if(append || (d.length>0 && pageT > 1)){
		    $(".page-wrapper").show();
		    $(".page-num").remove();
		    for(var i = 1 ; i <= pageT ; i++){
		        var row = $('<a class="page-num" href="#"></a>');
		        row.text(i);
		        row.attr("name","page-"+(i-1));
		        row.insertBefore($(".next-page"));
		        if(pageIndex == (i-1)){
		           row.addClass("now");
		        }
		    }
		    $(".page-num").click(function(){
		            if(!$(this).hasClass("now")){
		                pageIndex = parseInt($(this).text())-1;
		                getBooks($("input[name=ccode]").val(),pageSize,true);
		            }
		    });
		}else{
		    $(".page-wrapper").hide();
		}
	});
  }
  $(function(){
   //根据tid请求category
   request("listCategory.do","pageIndex=0&pageSize=30&isNotPage=0&pid="+$("input[name=tid]").val(),function(data){
		var d = data.data;
		for(var i = 0 ; i <d.length; i++){
		    var row = $('<span></span>');
            var cate = d[i];
            row.text(cate.name);
            row.attr("id",cate.id);
            row.attr("code",cate.code);
            if(i == 0){
               row.addClass("on");
            }
            $(".book-types-content-nav").append(row);
		}
		$("input[name=ccode]").val($(".book-types-content-nav span").first().attr("code"));
		getBooks($(".book-types-content-nav span").first().attr("code"),pageSize,false);
		//为每一个span绑定事件
		$(".book-types-content-nav span").click(function(){
		    $(".book-types-content-nav span").removeClass("on");
		    $(this).addClass("on");
		    pageIndex = 0;
		    pageT = 0;
		    $("input[name=ccode]").val($(this).attr("code"));
		    getBooks($(this).attr("code"),pageSize,false);
		})
	});
	
	       $(".pre-page").click(function(){
		        if(pageIndex == 0){
		            alert("当前已经是第一页");
		            return;
		        }
		        pageIndex--;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        });
		        getBooks($("input[name=ccode]").val(),pageSize,true);
		    });
		    
		    $(".next-page").click(function(){
		        if(pageIndex == pageT-1){
		            alert("当前已经是最后一页");
		            return;
		        }
		        pageIndex++;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        })
		        getBooks($("input[name=ccode]").val(),pageSize,true);
		   });
})


  
  </script>
 
</html>